﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.Models
{
    public class TrayCalculator
    {
        private readonly PointF[] corners;
        private const int Rows = 4;
        private const int Columns = 8;

        //离心管托盘1点位 （不带离心管帽）
        public TrayCalculator(int Type)
        {

            //type==1 托盘1点位(不带离心管帽)
            //type==2 托盘2点位(不带离心管帽)
            //type==3 托盘1点位(带离心管帽)
            //type==4 托盘2点位(带离心管帽)

            //放214.655,369.729

            if (Type == 1)
            {

                corners = new PointF[4] {
            new PointF(217.905f,367.479f), // 左下角
            new PointF(222.308f,546.208f), // 右下角
            new PointF(151.008f,547.12f), // 右上角
            new PointF(146.723f,368.729f) // 左上角
            };
            }

            if (Type == 2)
            {

                corners = new PointF[4] {
            new PointF(109.02f,370.812f), // 左下角
            new PointF(113.683f,549.144f), // 右下角
            new PointF(41.6147f,550.299f), // 右上角
            new PointF(36.6036f,371.803f) // 左上角
            };
            }


            if (Type == 3)
            {
                corners = new PointF[4] {
            new PointF(218.347f,368.196f), // 左下角
            new PointF(222.532f,546.287f), // 右下角
            new PointF(151.361f,548.096f), // 右上角
            new PointF(146.03f,369.58f) // 左上角

            };
            }


            if (Type == 4)
            {
                corners = new PointF[4] {
            new PointF(108.717f,370.079f), // 左下角
            new PointF(112.468f,548.48f), // 右下角
            new PointF(40.5544f,550.615f), // 右上角
            new PointF(36.8015f,371.999f) // 左上角

                };
            }

            //if (Type == 4)
            //{
            //    corners = new PointF[4] {
            //new PointF(215.464f, 365.757f), // 左下角
            //new PointF(222.977f, 544.760f), // 右下角
            //new PointF(148.95f, 547.052f), // 右上角
            //new PointF(142.243f, 367.069f) // 左上角

            //    };
            //}


        }



        public PointF GetPosition(int positionNumber)
        {
            if (positionNumber < 1 || positionNumber > Rows * Columns)
                throw new ArgumentOutOfRangeException("点位编号必须在1-32之间");

            // 计算行列索引(0-based)
            int col = (positionNumber - 1) / Rows;  // 列优先计算
            int row = (positionNumber - 1) % Rows;

            // Z字形排列：奇数列从下到上，偶数列从上到下
            if (col % 2 == 1)
            {
                row = Rows - 1 - row;
            }

            // 计算插值比例
            float xRatio = (float)col / (Columns - 1);
            float yRatio = (float)row / (Rows - 1);

            // 双线性插值计算坐标
            PointF bottom = Lerp(corners[0], corners[1], xRatio);
            PointF top = Lerp(corners[3], corners[2], xRatio);
            return Lerp(bottom, top, yRatio);
        }

        private PointF Lerp(PointF a, PointF b, float t)
        {
            return new PointF(
                a.X + (b.X - a.X) * t,
                a.Y + (b.Y - a.Y) * t);
        }
    }

    public struct PointF
    {
        public float X { get; set; }
        public float Y { get; set; }

        public PointF(float x, float y)
        {
            X = x;
            Y = y;
        }
    }
}
